package com.company.thread;


import java.io.*;
import java.util.concurrent.locks.LockSupport;

/**
 * @description: 演示 LockSupport.park()方法让程序阻塞并用 Thread.interrupted()打断
 * @author: ruoan
 * @date: 2020/10/24 17:07
 */
public class ParkDemo2 {



    public static void main(String[] args) throws InterruptedException, IOException {

        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                LockSupport.park();
                // step1: 读取step1.json
                File file = new File("step1.json");
                try {
                    InputStream inputStream = new FileInputStream(file);
                    byte[] buf = new byte[1024];
                    while (inputStream.read(buf)!=-1){
                        System.out.println(new String(buf));
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Thread.interrupted(); // 打断标记从 true->false, 下一步的park才能阻塞
                LockSupport.park();
                System.out.println("thread end");

            }
        },"t");
        t.start();



        //创建step1.json
        File f = new File("step1.json");
        String content = new String("hello");
        byte[] bytes = content.getBytes();
        OutputStream stream = new FileOutputStream(f);
        stream.write(bytes);
        t.interrupt(); // 把t的打断标记 false->true, 此时 LockSupport.park() 会中断阻塞





    }
}
